#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int maxSubarraySumCircular(vector<int>& nums) {

        int n = nums.size();
        vector<vector<int>> dp(2, vector<int>(n + 1, 0));

        dp[0][0] = 0;
        dp[1][0] = 0;
        int sum = 0;


        for (int i = 1; i < n + 1; i++)
        {
            dp[0][i] = max(dp[0][i - 1] + nums[i - 1], nums[i - 1]);
            dp[1][i] = min(dp[1][i - 1] + nums[i - 1], nums[i - 1]);
            sum = nums[i - 1] + sum;

        }

        int resmax = INT_MIN;
        int resmin = INT_MAX;

        for (int i = 1; i < n + 1; i++)
        {
            resmax = max(dp[0][i], resmax);
            resmin = min(dp[1][i], resmin);
        }

        return sum == resmin ? resmax : max(resmax, sum - resmin);
    }
};